using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using System.Collections;
using Inline.SharePoint.ExtendedVariations.Helpers;

namespace Inline.SharePoint.ExtendedVariations.Features.Inline.SharePoint.ExtendedVariations
{

    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>
    [Guid("b0b99549-b90b-400d-8bfe-7cd690c1834b")]
    public class InlineSharePointEventReceiver : SPFeatureReceiver
    {

        private EventRegistrationHandler _eventRegistrationHandler = new EventRegistrationHandler();
        private const string _logListName = "Inline.SharePoint.ExtendedVariations.Logs";

        /// <summary>
        /// Register events for all existing lists in the web
        /// </summary>
        /// <param name="properties"></param>
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {

            SPWeb web = properties.Feature.Parent as SPWeb; // Web feature

            // Create new list to store error messages
            SPList list = web.Lists.TryGetList(_logListName);
            if (list == null)
            {

                Guid listId = web.Lists.Add(_logListName,
                    "Holds the Inline.SharePoint.ExtendedVariations log messages",
                    SPListTemplateType.GenericList);
                list = web.Lists[listId];

                list.Fields.Add("Message", SPFieldType.Note, true);
                list.Fields.Add("LogLevel", SPFieldType.Text, true);

                list.Update();

            }

            // For each list, register the necessary events
            SPListCollection lColl = web.Lists;
            for (int i = 0; i < lColl.Count; i++)
            {

                if (!lColl[i].Hidden)
                {

                    if (lColl[i].Title != _logListName) // No replication needed for log list
                    {

                        foreach (SPEventReceiverType rType in _eventRegistrationHandler.EventTypeClassRelation.Keys)
                        {

                            _eventRegistrationHandler.DeleteRegisteredEventReceiver(lColl[i],
                                rType,
                                _eventRegistrationHandler.EventTypeClassRelation[rType].ToString());

                            if (!_eventRegistrationHandler.ListAlreadyHasEventReceiverRegistered(lColl[i],
                                rType,
                                _eventRegistrationHandler.EventTypeClassRelation[rType].ToString()))
                            {

                                lColl[i].EventReceivers.Add(rType,
                                    _eventRegistrationHandler.Assembly,
                                    _eventRegistrationHandler.EventTypeClassRelation[rType].ToString());

                            }

                        }

                        lColl[i].Update();

                    }

                }

            }

        }

        /// <summary>
        /// Unregister events for all existing lists in the web
        /// </summary>
        /// <param name="properties"></param>
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {

            SPWeb web = properties.Feature.Parent as SPWeb; // Web feature

            // Delete logging list
            SPList list = web.Lists.TryGetList(_logListName);
            if (list != null)
                list.Delete();

            // For each list, unregister the necessary events
            SPListCollection lColl = web.Lists;
            for (int i = 0; i < lColl.Count; i++)
            {

                if (!lColl[i].Hidden)
                {

                    foreach (SPEventReceiverType rType in _eventRegistrationHandler.EventTypeClassRelation.Keys)
                    {

                        _eventRegistrationHandler.DeleteRegisteredEventReceiver(lColl[i],
                            rType,
                            _eventRegistrationHandler.EventTypeClassRelation[rType].ToString());

                    }

                    lColl[i].Update();

                }

            }

        }


        // Uncomment the method below to handle the event raised after a feature has been installed.

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        // Uncomment the method below to handle the event raised when a feature is upgrading.

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}

    }

}